<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>$Injector API</title>
<link rel="stylesheet" href="../lib/bootstrap3/css/bootstrap.min.css">
<link rel="stylesheet" href="../lib/bootstrap3/css/dashboard.css">
<script src="../lib/angularjs/angular.min.js"></script>
</head>
<body>
<h2>$Injector API</h2>
<div ng-app="app">
    <div class="well row" ng-controller="ctrl01">
        <div class="col-sm-4">
            <h4>推断式注入声明</h4>
            <button class="btn-success btn" ng-click="sayHello()">点击</button>
            <button class="btn-success btn" ng-click="dependencyService()">查看依赖的服务</button>
            <button class="btn-success btn" ng-click="getU()">查看用户</button>
            <div class="alert alert-danger"><h2>{{message}}</h2></div>
            <div class="alert alert-info">这种注入方式，需要在保证参数名称与服务名称相同。如果代码要经过压缩等操作，就会导致注入失败。</div>
        </div>
        <div class="col-sm-8">
            <xmp>
                app.controller('ctrl01',ctrl01);
                function ctrl01(a,b) {
                    a.message=b.great(Math.random());
                    a.sayHello=function () {/** 点击事件  **/
                        a.message=b.great(Math.random());
                    };
                    a.dependencyService=function () {/** 当前依赖的服务 **/
                        var $injector=angular.injector();
                        a.message=$injector.annotate(ctrl01);
                    }
                };
                ctrl01.$inject = ['$scope', 'greater'];
            </xmp>
        </div>
    </div>
</div>
<script>
    var app = angular.module("app", []);
    app.factory('greater',function () {
        return {
            great:function (msg) {
                return ('Hello '+msg);
            }
        }
    });
</script>
<script>
    app.controller('ctrl01',ctrl01);
    app.service('UserService',function () {
        this.getById=function(id){
            return {id:id};
        };
    });
    function ctrl01(a,b,c) {
        a.message=b.great(Math.random());
        a.sayHello=function () {/** 点击事件  **/
            a.message=b.great(Math.random());
        };
        a.dependencyService=function () {/** 当前依赖的服务 **/
            var $injector=angular.injector();
            a.message=$injector.annotate(ctrl01);
        };
        a.getU=function () {
            a.message=c.getById(Math.random());
        }
    };
    ctrl01.$inject = ['$scope', 'greater','UserService'];
</script>
</body>
</html>